package com.dylearn.test.file;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @CreateBy zhangdingyi
 * @Date 2022-07-31
 * @Content
 */
@SpringBootTest
public class FileTestUse {

    /**
     * @Author zhangdingyi
     * @Date 2022-07-31 22:36
     * @Content 导入数据 --由一个文件拆分成多个文件  --这里有一个问题，就是如果正常的文件拆分成多个文件，那么其分出的模块文件都是无法正常使用的
     */
    @Test
    void importData() {
        //输入流 --字节流
        InputStream fileInputStream = null;
        //BufferedInputStream bufferedInputStream = null;
        //输出流 --缓存字符流
        //BufferedReader bufferedReader = null;
        //输出流 --缓存字节流
        OutputStream fileOutputStream = null;
        //BufferedOutputStream bufferedOutputStream = null;
        //输出流 --
        // FileWriter fileWriter = null;

        //原路径
        String yUrl = "D:" +
                File.separator + "java学习" +
                File.separator + "java基础" +
                File.separator + "java常见应用" +
                File.separator + "IO流拆分合并文件.docx";

        //现在路径
        String xUrl = "D:" +
                File.separator + "java学习" +
                File.separator + "java基础" +
                File.separator + "java常见应用" +
                File.separator + "拆分文件" +
                File.separator + "IO流拆分合并文件";
        OutputStream outputStream = null;

        try {
            //读取一个文件
            fileInputStream = new FileInputStream(yUrl);

            //转换成字符缓存流 ---不转了
            //bufferedReader = new BufferedReader(inputStream); ---直接转换会报错，它需要一个Reader类
            //bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            //直接使用字节流保证数据的完整性

            //缓存进行拆分
            byte[] bytes = new byte[1024 * 1024];
            int len = -1;
            int num = 1;
            while ((len = fileInputStream.read(bytes)) != -1) {
                //输出字符流 写文件出来 //输出流进行输出
                fileOutputStream = new FileOutputStream(xUrl + (num++) + ".docx");
                fileOutputStream.write(bytes, 0, len);
            }

            //生成配置文件 1
            /*
            fileOutputStream = new FileOutputStream(xUrl + "配置文件" + num);
            fileOutputStream.write(("name=" + "IO流拆分合并文件.docx").getBytes());
            //换行符
            fileOutputStream.write(System.getProperty("line.separator").getBytes());
            fileOutputStream.write(("num=" + num).getBytes());*/

            //生成配置文件 2
            Properties properties = new Properties();
            properties.setProperty("name", "test.docx");
            properties.setProperty("num", num + "");
            properties.store(new FileOutputStream(xUrl + "配置文件" + num + ".properties"), "IO流拆分合并文件配置");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                //先关出，再关入，从 外 往 内 关
                if (fileOutputStream != null) fileOutputStream.close();
                if (fileInputStream != null) fileInputStream.close();
                System.out.println("文件成功写出！");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * @Author zhangdingyi
     * @Date 2022-08-01 00:11
     * @Content 拆分数据整合成一个完整的文件
     */
    @Test
    void exportData() throws IOException {
        //输入流 --字节流
        FileInputStream fileInputStream = null;
        //输出流 --缓存字节流
        FileOutputStream fileOutputStream = null;

        //拆分路径
        String xUrl = "D:" +
                File.separator + "java学习" +
                File.separator + "java基础" +
                File.separator + "java常见应用" +
                File.separator + "拆分文件";

        //合并路径
        String yUrl = "D:" +
                File.separator + "java学习" +
                File.separator + "java基础" +
                File.separator + "java常见应用" +
                File.separator + "合并文件";

        List<FileInputStream> inputStreamList = null;

        try {
            inputStreamList = new ArrayList<>();
            //先读取配置文件
            Properties properties = new Properties();
            properties.load(new FileInputStream(xUrl + File.separator + "IO流拆分合并文件配置文件5.properties"));
            int num = Integer.parseInt(properties.getProperty("num"));
            String name = properties.getProperty("name");

            for (int i = 1; i < num; i++) {
                fileInputStream = new FileInputStream(xUrl + File.separator + "IO流拆分合并文件" + i + ".docx");
                inputStreamList.add(fileInputStream);

            }

            fileOutputStream = new FileOutputStream(yUrl + File.separator + name);

            //缓存进行拆分
            byte[] bytes = new byte[1024 * 1024];
            for (FileInputStream inputStream : inputStreamList) {
                //把数据读取到缓存中
                int len = inputStream.read(bytes);
                //再把缓存数据读取到输出流 --这里直接进行读取缓存并存储到对应的文件夹中，文件依旧保持完好！
                fileOutputStream.write(bytes, 0, len);
            }


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                //先关出，再关入，从 外 往 内 关
                if (fileOutputStream != null) fileOutputStream.close();
                if (fileInputStream != null) fileInputStream.close();
                for (FileInputStream inputStream : inputStreamList) {
                    if (inputStream != null) inputStream.close();
                }
                System.out.println("文件整合成功！！");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }
}
